<template>
  <div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{ path: '/sys-admin' }">后台管理</el-breadcrumb-item>
    <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
  </el-breadcrumb>
    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="110" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="100" align="center"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" width="100" align="center"></el-table-column>
      <el-table-column prop="logo" label="LOGO" width="100" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键字列表" width="100" align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
      <el-table-column prop="sales" label="销量" width="100" align="center"></el-table-column>
      <el-table-column prop="productCount" label="库存" width="100" align="center"></el-table-column>
      <el-table-column prop="commentCount" label="评论量" width="80" align="center"></el-table-column>
      <el-table-column prop="positiveCommentCount" label="好评量" width="100" align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="100" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              :disabled="scope.row.id == 1"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#AAAAAA">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center" >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete"
                     circle size="mini" :disabled="scope.row.id == 1" @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出的编辑品牌的对话框 -->
    <el-dialog title="修改品牌" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="拼音" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.pinyin"></el-input>
        </el-form-item>
        <el-form-item label="LOGO" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.logo"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="关键字" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.keywords"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="销量" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.sales"></el-input>
        </el-form-item>
        <el-form-item label="库存" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.productCount"></el-input>
        </el-form-item>
        <el-form-item label="评论量" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.commentCount"></el-input>
        </el-form-item>
        <el-form-item label="好评量" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.positiveCommentCount"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.enable"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      ruleForm: {
        name: '',
        pinyin: '',
        logo: '',
        description: '',
        keywords: '',
        sort: '',
        sales: '',
        productCount: '',
        commentCount: '',
        positiveCommentCount: '',
        enable: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    submitEdit() {
      let url = 'http://localhost:9080/brands/' + this.ruleForm.id +'/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改品牌成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadBrandList();
        } else if (responseBody.state == 40900) {
          this.$message.error(responseBody.message);
        } else {
          this.$message.error(responseBody.message);
          this.dialogFormVisible = false;
          this.loadBrandList();
        }
      });
    },
    loadBrandList() {
      console.log('loadBrandList');
      let url = 'http://localhost:9080/brands';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    },
    handleEdit(brand){
      let message = '您正在尝试编辑【' + brand.id + '-' + brand.name + '】的相册详情……';
      console.log(message);
      // this.dialogFormVisible = true;
      // this.ruleForm = brand;
      let url = 'http://localhost:9080/brands/' + brand.id;
      console.log(url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.ruleForm = responseBody.data;
          this.dialogFormVisible = true;
        } else {
          this.$message.error(responseBody.message);
          this.loadBrandList();
        }
      });
    },
    handleDelete(brand) {
      let url = 'http://localhost:9080/brands/' + brand.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 20000 || responseBody.state == 40400) {
          this.loadAlbumList();
        }
      });
    },
    openDeleteConfirm(brand) {
      let title = '提示';
      let message = '此操作将永久删除[' + brand.name + '] 品牌，是否继续？';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand);
      }).catch(() => {
      });
    },
  },
  mounted() {
    console.log('mounted');
    this.loadBrandList();
  }
}
</script>

